﻿using System;
using System.Collections.Generic;
using System.Xml;
using Autodesk.Revit.DB;
using YJKArch.ImportStructureProject.ImportObjects;
using YJKArch.Utils;

namespace YJKArch.ImportStructureProject
{
	public class ReadStructureXMLFile
	{
		public ReadStructureXMLFile(ref List<ElementId> oldColumnsId, ref List<ElementId> oldBeamsId, ref List<ElementId> oldWallsId, ref List<ElementId> oldFloorsId, ref List<BeamInfos> beamStructureData, ref List<ColumnInfos> columnStructureData, ref List<WallInfos> wallStructureData, ref List<FloorInfos> floorStructureData)
		{
			this.m_OldColumnsId = oldColumnsId;
			this.m_OldBeamsId = oldBeamsId;
			this.m_OldWallsId = oldWallsId;
			this.m_OldFloorsId = oldFloorsId;
			this.m_BeamStructureData = beamStructureData;
			this.m_ColumnStructureData = columnStructureData;
			this.m_WallStructureData = wallStructureData;
			this.m_FloorStructureData = floorStructureData;
		}

		public bool ReadStructureInfo(string structureInfoFilePath, ref string errMessage)
		{
			if (structureInfoFilePath.Substring(structureInfoFilePath.Length - 3).ToUpper().CompareTo("XML") != 0)
			{
				errMessage = "不是合法的结构输出文件！";
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(structureInfoFilePath);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("TSZStructData");
			if (xmlNode == null)
			{
				errMessage = "结构输出文件格式不符！";
				return false;
			}
			XmlNode xmlNode2 = xmlNode.SelectSingleNode("梁信息");
			if (xmlNode2 == null)
			{
				errMessage = "结构输出文件缺少梁数据！";
				return false;
			}
			if (!this.ReadStructureBeamInfo(xmlNode2, ref errMessage))
			{
				return false;
			}
			XmlNode xmlNode3 = xmlNode.SelectSingleNode("结构柱信息");
			if (xmlNode3 == null)
			{
				errMessage = "结构输出文件缺少柱数据！";
				return false;
			}
			if (!this.ReadStructureColumnInfo(xmlNode3, ref errMessage))
			{
				return false;
			}
			XmlNode xmlNode4 = xmlNode.SelectSingleNode("剪力墙信息");
			if (xmlNode4 == null)
			{
				errMessage = "结构输出文件缺少墙数据！";
				return false;
			}
			if (!this.ReadStructureWallInfo(xmlNode4, ref errMessage))
			{
				return false;
			}
			XmlNode xmlNode5 = xmlNode.SelectSingleNode("板信息");
			if (xmlNode5 == null)
			{
				errMessage = "结构输出文件缺少楼板数据！";
				return false;
			}
			if (!this.ReadStructureFloorInfo(xmlNode5, ref errMessage))
			{
				return false;
			}
			XmlNode nodeDeleteComponent = xmlNode.SelectSingleNode("删除构件");
			return xmlNode5 == null || this.ReadDeleteComponentInfo(nodeDeleteComponent, ref errMessage);
		}

		private bool ReadStructureBeamInfo(XmlNode nodeBeam, ref string errMessage)
		{
			foreach (object obj in nodeBeam.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (((XmlElement)xmlNode).LocalName.CompareTo("梁数量") != 0)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Rac_ID");
					ElementId oldElementId = null;
					if (xmlNode2.InnerText.CompareTo("") != 0)
					{
						oldElementId = new ElementId(Convert.ToInt32(xmlNode2.InnerText));
					}
					XmlNode xmlNode3 = xmlNode.SelectSingleNode("起点");
					XYZ pointInfoByString = this.GetPointInfoByString(xmlNode3.InnerText);
					XmlNode xmlNode4 = xmlNode.SelectSingleNode("终点");
					XYZ pointInfoByString2 = this.GetPointInfoByString(xmlNode4.InnerText);
					double bulge = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("凸度").InnerText));
					PoleSectionStyle sectionStyle = (PoleSectionStyle)Convert.ToInt32(xmlNode.SelectSingleNode("截面类型").InnerText);
					double width = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("截面参数1").InnerText));
					double height = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("截面参数2").InnerText));
					BeamInfos item = new BeamInfos(oldElementId, pointInfoByString, pointInfoByString2, bulge, sectionStyle, width, height);
					this.m_BeamStructureData.Add(item);
				}
			}
			return true;
		}

		private bool ReadStructureColumnInfo(XmlNode nodeColumn, ref string errMessage)
		{
			foreach (object obj in nodeColumn.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (((XmlElement)xmlNode).LocalName.CompareTo("结构柱数量") != 0)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Rac_ID");
					ElementId oldElementId = null;
					if (xmlNode2.InnerText.CompareTo("") != 0)
					{
						oldElementId = new ElementId(Convert.ToInt32(xmlNode2.InnerText));
					}
					XmlNode xmlNode3 = xmlNode.SelectSingleNode("定位点");
					XYZ pointInfoByString = this.GetPointInfoByString(xmlNode3.InnerText);
					double rotation = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("角度").InnerText));
					double height = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("柱高").InnerText));
					PoleSectionStyle sectionStyle = (PoleSectionStyle)Convert.ToInt32(xmlNode.SelectSingleNode("截面类型").InnerText);
					double b = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("截面参数1").InnerText));
					double h = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("截面参数2").InnerText));
					ColumnInfos item = new ColumnInfos(oldElementId, pointInfoByString, rotation, height, sectionStyle, b, h);
					this.m_ColumnStructureData.Add(item);
				}
			}
			return true;
		}

		private bool ReadStructureWallInfo(XmlNode nodeWall, ref string errMessage)
		{
			foreach (object obj in nodeWall.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (((XmlElement)xmlNode).LocalName.CompareTo("剪力墙数量") != 0)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Rac_ID");
					ElementId oldElementId = null;
					if (xmlNode2.InnerText.CompareTo("") != 0)
					{
						oldElementId = new ElementId(Convert.ToInt32(xmlNode2.InnerText));
					}
					XmlNode xmlNode3 = xmlNode.SelectSingleNode("起点");
					XYZ pointInfoByString = this.GetPointInfoByString(xmlNode3.InnerText);
					XmlNode xmlNode4 = xmlNode.SelectSingleNode("终点");
					XYZ pointInfoByString2 = this.GetPointInfoByString(xmlNode4.InnerText);
					double bulge = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("凸度").InnerText));
					double height = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("墙高").InnerText));
					double thickness = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("墙厚").InnerText));
					XmlNode xmlNode5 = xmlNode.SelectSingleNode("墙洞");
					List<WallHoleInfos> holes = new List<WallHoleInfos>();
					if (xmlNode5 != null)
					{
						this.ReadStructureWallHoleInfo(xmlNode5, ref holes, ref errMessage);
					}
					WallInfos item = new WallInfos(oldElementId, pointInfoByString, pointInfoByString2, bulge, height, thickness, holes);
					this.m_WallStructureData.Add(item);
				}
			}
			return true;
		}

		private bool ReadStructureWallHoleInfo(XmlNode nodeWallHole, ref List<WallHoleInfos> wallHoles, ref string errMessage)
		{
			foreach (object obj in nodeWallHole.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (((XmlElement)xmlNode).LocalName.CompareTo("墙洞数量") != 0)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Rac_ID");
					ElementId oldElementId = null;
					if (xmlNode2.InnerText.CompareTo("") != 0)
					{
						oldElementId = new ElementId(Convert.ToInt32(xmlNode2.InnerText));
					}
					XmlNode xmlNode3 = xmlNode.SelectSingleNode("点1");
					XYZ pointInfoByString = this.GetPointInfoByString(xmlNode3.InnerText);
					XmlNode xmlNode4 = xmlNode.SelectSingleNode("点2");
					XYZ pointInfoByString2 = this.GetPointInfoByString(xmlNode4.InnerText);
					WallHoleInfos item = new WallHoleInfos(oldElementId, pointInfoByString, pointInfoByString2);
					wallHoles.Add(item);
				}
			}
			return true;
		}

		private bool ReadStructureFloorInfo(XmlNode nodeFloor, ref string errMessage)
		{
			foreach (object obj in nodeFloor.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (((XmlElement)xmlNode).LocalName.CompareTo("板数量") != 0)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Rac_ID");
					ElementId oldElementId = null;
					if (xmlNode2.InnerText.CompareTo("") != 0)
					{
						oldElementId = new ElementId(Convert.ToInt32(xmlNode2.InnerText));
					}
					double thickness = Common.MMToFeet(Convert.ToDouble(xmlNode.SelectSingleNode("板厚").InnerText));
					List<EdgeInfos> list = new List<EdgeInfos>();
					foreach (object obj2 in xmlNode.SelectSingleNode("板边"))
					{
						XmlElement xmlElement = (XmlElement)((XmlNode)obj2);
						if (xmlElement.LocalName.CompareTo("板边数量") != 0)
						{
							XmlNode xmlNode3 = xmlElement.SelectSingleNode("起点");
							XYZ pointInfoByString = this.GetPointInfoByString(xmlNode3.InnerText);
							XmlNode xmlNode4 = xmlElement.SelectSingleNode("终点");
							XYZ pointInfoByString2 = this.GetPointInfoByString(xmlNode4.InnerText);
							double bulge = Convert.ToDouble(xmlElement.SelectSingleNode("凸度").InnerText);
							EdgeInfos item = new EdgeInfos(pointInfoByString, pointInfoByString2, bulge);
							list.Add(item);
						}
					}
					XmlNode xmlNode5 = xmlNode.SelectSingleNode("板洞口");
					List<FloorHoleInfos> holes = new List<FloorHoleInfos>();
					if (xmlNode5 != null)
					{
						this.ReadStructureFloorHoleInfo(xmlNode5, ref holes, ref errMessage);
					}
					FloorInfos item2 = new FloorInfos(oldElementId, list, holes, thickness);
					this.m_FloorStructureData.Add(item2);
				}
			}
			return true;
		}

		private bool ReadStructureFloorHoleInfo(XmlNode nodeFloorHoles, ref List<FloorHoleInfos> floorHoles, ref string errMessage)
		{
			foreach (object obj in nodeFloorHoles.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (((XmlElement)xmlNode).LocalName.CompareTo("板洞口数量") != 0)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode("Rac_ID");
					ElementId oldElementId = null;
					if (xmlNode2.InnerText.CompareTo("") != 0)
					{
						oldElementId = new ElementId(Convert.ToInt32(xmlNode2.InnerText));
					}
					List<EdgeInfos> list = new List<EdgeInfos>();
					foreach (object obj2 in xmlNode.SelectSingleNode("板洞口边"))
					{
						XmlElement xmlElement = (XmlElement)((XmlNode)obj2);
						if (xmlElement.LocalName.CompareTo("板洞口边数量") != 0)
						{
							XmlNode xmlNode3 = xmlElement.SelectSingleNode("起点");
							XYZ pointInfoByString = this.GetPointInfoByString(xmlNode3.InnerText);
							XmlNode xmlNode4 = xmlElement.SelectSingleNode("终点");
							XYZ pointInfoByString2 = this.GetPointInfoByString(xmlNode4.InnerText);
							double bulge = Convert.ToDouble(xmlElement.SelectSingleNode("凸度").InnerText);
							EdgeInfos item = new EdgeInfos(pointInfoByString, pointInfoByString2, bulge);
							list.Add(item);
						}
					}
					FloorHoleInfos item2 = new FloorHoleInfos(oldElementId, list);
					floorHoles.Add(item2);
				}
			}
			return true;
		}

		private bool ReadDeleteComponentInfo(XmlNode nodeDeleteComponent, ref string errMessage)
		{
			XmlNode xmlNode = nodeDeleteComponent.SelectSingleNode("梁");
			if (xmlNode != null)
			{
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					if (((XmlElement)xmlNode2).LocalName.CompareTo("梁数量") != 0)
					{
						XmlNode xmlNode3 = xmlNode2.SelectSingleNode("Rac_ID");
						ElementId item = null;
						if (xmlNode3.InnerText.CompareTo("") != 0)
						{
							item = new ElementId(Convert.ToInt32(xmlNode3.InnerText));
						}
						this.m_OldBeamsId.Add(item);
					}
				}
			}
			XmlNode xmlNode4 = nodeDeleteComponent.SelectSingleNode("结构柱");
			if (xmlNode4 != null)
			{
				foreach (object obj2 in xmlNode4.ChildNodes)
				{
					XmlNode xmlNode5 = (XmlNode)obj2;
					if (((XmlElement)xmlNode5).LocalName.CompareTo("结构柱数量") != 0)
					{
						XmlNode xmlNode6 = xmlNode5.SelectSingleNode("Rac_ID");
						ElementId item2 = null;
						if (xmlNode6.InnerText.CompareTo("") != 0)
						{
							item2 = new ElementId(Convert.ToInt32(xmlNode6.InnerText));
						}
						this.m_OldColumnsId.Add(item2);
					}
				}
			}
			XmlNode xmlNode7 = nodeDeleteComponent.SelectSingleNode("剪力墙");
			if (xmlNode7 != null)
			{
				foreach (object obj3 in xmlNode7.ChildNodes)
				{
					XmlNode xmlNode8 = (XmlNode)obj3;
					if (((XmlElement)xmlNode8).LocalName.CompareTo("剪力墙数量") != 0)
					{
						XmlNode xmlNode9 = xmlNode8.SelectSingleNode("Rac_ID");
						ElementId item3 = null;
						if (xmlNode9.InnerText.CompareTo("") != 0)
						{
							item3 = new ElementId(Convert.ToInt32(xmlNode9.InnerText));
						}
						this.m_OldWallsId.Add(item3);
					}
				}
			}
			XmlNode xmlNode10 = nodeDeleteComponent.SelectSingleNode("板");
			if (xmlNode10 != null)
			{
				foreach (object obj4 in xmlNode10.ChildNodes)
				{
					XmlNode xmlNode11 = (XmlNode)obj4;
					if (((XmlElement)xmlNode11).LocalName.CompareTo("板数量") != 0)
					{
						XmlNode xmlNode12 = xmlNode11.SelectSingleNode("Rac_ID");
						ElementId item4 = null;
						if (xmlNode12.InnerText.CompareTo("") != 0)
						{
							item4 = new ElementId(Convert.ToInt32(xmlNode12.InnerText));
						}
						this.m_OldFloorsId.Add(item4);
					}
				}
			}
			return true;
		}

		private XYZ GetPointInfoByString(string strXYZ)
		{
			int num = strXYZ.IndexOf(",");
			string value = strXYZ.Substring(0, num);
			strXYZ = strXYZ.Substring(num + 1, strXYZ.Length - num - 1);
			num = strXYZ.IndexOf(",");
			string value2 = strXYZ.Substring(0, num);
			string value3 = strXYZ.Substring(num + 1, strXYZ.Length - num - 1);
			double val = Convert.ToDouble(value);
			double val2 = Convert.ToDouble(value2);
			double val3 = Convert.ToDouble(value3);
			return new XYZ(Common.MMToFeet(val), Common.MMToFeet(val2), Common.MMToFeet(val3));
		}

		private List<ElementId> m_OldColumnsId;

		private List<ElementId> m_OldBeamsId;

		private List<ElementId> m_OldWallsId;

		private List<ElementId> m_OldFloorsId;

		private List<BeamInfos> m_BeamStructureData;

		private List<ColumnInfos> m_ColumnStructureData;

		private List<WallInfos> m_WallStructureData;

		private List<FloorInfos> m_FloorStructureData;
	}
}
